 package com.oanda.blackberry.view;
 
 import com.oanda.blackberry.restapi.Price;
 import com.oanda.blackberry.restapi.Rate;
 import com.oanda.blackberry.restapi.Symbol;
 import com.oanda.blackberry.restapi.User;
 import com.oanda.blackberry.util.AbstractController;
 import com.oanda.blackberry.util.AbstractViewPanel;
 import com.oanda.blackberry.util.DefaultController;
 import com.oanda.blackberry.util.Model;
 import com.oanda.blackberry.util.PropertyChangeEvent;
 import com.oanda.blackberry.util.Settings;
 import com.oanda.blackberry.util.log.Logger;
 import java.util.Timer;
 import java.util.TimerTask;
 import net.rim.device.api.system.Application;
 import net.rim.device.api.ui.Field;
 import net.rim.device.api.ui.FieldChangeListener;
 import net.rim.device.api.ui.MenuItem;
 import net.rim.device.api.ui.Screen;
 import net.rim.device.api.ui.UiApplication;
 import net.rim.device.api.ui.component.LabelField;
 import net.rim.device.api.ui.component.Menu;
 import net.rim.device.api.ui.container.MainScreen;
 
 public class MainView extends MainScreen
   implements AbstractViewPanel
 {
   DefaultController controller;
   Timer t = new Timer();
   TimerTask task = new TimerTask()
   {
     public void run() {
       MainView.this.controller.accountList(Model.getModel().getSessionToken(), Model.getModel().getUser().getUsername());
     }
   };
 
   MenuItem getAccountsMenuItem = new MenuItem("Get Accounts", 1000, 19997)
   {
     public void run()
     {
       MainView.this.controller.accountList(Model.getModel().getSessionToken(), Model.getModel().getUser().getUsername());
     }
   };
 
   MenuItem showListSymbolsScreenMenuItem = new MenuItem("Show list symbols screen", 1000, 19998)
   {
     public void run() {
       MainView.this.controller.propertyChange(new PropertyChangeEvent(this, "showListSymbols", MainView.this.controller, MainView.this.controller));
     }
   };
 
   MenuItem listSymbolsMenuItem = new MenuItem("Rates list symbols", 1000, 19998)
   {
     public void run() {
       MainView.this.controller.listSymbols(Model.getModel().getSessionToken());
     }
   };
   Timer tListSymbols;
   TimerTask tListSymbolsTask = new TimerTask() {
     public void run() {
       MainView.this.controller.listSymbols(Model.getModel().getSessionToken());
     }
   };
 
   MenuItem listSymbolsMultyMenuItem = new MenuItem("Rates list symbols Multy", 1000, 19998)
   {
     public void run() {
       if (MainView.this.tListSymbols == null) {
         MainView.this.tListSymbols = new Timer();
         MainView.this.tListSymbols.schedule(MainView.this.tListSymbolsTask, 0L, 10000L);
       }
     }
   };
 
   MenuItem ratesMenuItem = new MenuItem("Rates", 1000, 19999)
   {
     public void run() {
       Model.getModel().getRate(); long l = Rate.getSince();
       String timeStamp = null;
       if (l > 0L) {
         timeStamp = Long.toString(l);
       }
       MainView.this.controller.quote(Model.getModel().getSessionToken(), Model.getModel().getRateSymbolsSelected(), timeStamp);
     }
   };
 
   MenuItem showRatesMenuItem = new MenuItem("Show Rates View", 1000, 19999)
   {
     public void run() {
       MainView.this.controller.propertyChange(new PropertyChangeEvent(this, "showRatesView", MainView.this.controller, MainView.this.controller));
     }
   };
 
   MenuItem logoutMenuItem = new MenuItem("Log out", 1000, 20000)
   {
     public void run() {
       MainView.this.hide();
       MainView.this.controller.logout(Model.getModel().getSessionToken());
     }
   };
 
   MenuItem optionsMenuItem = new MenuItem("Options", 1000, 20001)
   {
     public void run() {
       MainView.this.controller.showOptions();
     }
   };
 
   MenuItem helpMenuItem = new MenuItem("Contact", 1000, 20002)
   {
     public void run() {
       MainView.this.controller.help();
     }
   };
 
   MenuItem aboutMenuItem = new MenuItem("About", 1000, 20003)
   {
     public void run() {
       MainView.this.controller.about();
     }
   };
 
   MenuItem closeMenuItem = new MenuItem("Close", 1000, 20004)
   {
     public void run() {
       MainView.this.controller.exit();
     }
   };
 
   public MainView()
   {
   }
 
   public MainView(DefaultController contr)
   {
     this.controller = contr;
     setTitle("Main View");
 
     HeaderField hft = new HeaderField("Symbol", "Bid", "Ask");
     hft.setWidth(300);
     hft.setBGColor(15658734);
     hft.setFontColorLeft(0);
     hft.setFontColorCenter(3381504);
     hft.setFontColorRight(10027008);
     add(hft);
 
     add(new TestField());
     add(new PriceField(65536L, new Price(new Symbol("tst/tst", 5, 0.0001D, true), 1.23456D)));
 
     add(new LabelField(String.valueOf(new Double(0.300000003D).doubleValue())));
     add(new LabelField(Double.toString(new Double(0.300000003D).doubleValue())));
 
     PriceField pr = new PriceField(65536L, new Price(new Symbol("t22/t12", 3, 0.1D, true), 1.765D));
     pr.setChangeListener(new FieldChangeListener()
     {
       public void fieldChanged(Field field, int context)
       {
         DefaultController.inform("Field Changed" + String.valueOf(context));
       }
     });
     add(pr);
 
     add(new LabelField("DEBUG ino:"));
   }
 
   private void addSessionToken(final String s)
   {
     UiApplication.getUiApplication().invokeLater(new Runnable() {
       private final String _s=s;
 
       public void run() { MainView.this.add(new LabelField("sessiontoken: " + this._s)); }
     });
   }
 
   public void modelPropertyChange(PropertyChangeEvent evt)
   {
     if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("setSettings"))) {
       if ((evt.getNewValue() != null) && ((evt.getNewValue() instanceof Settings))) {
         Settings s = (Settings)evt.getNewValue();
         if (s.getSessionToken() != null)
           addSessionToken(s.getSessionToken());
       }
     }
     else if (evt.getPropertyName().equals("sessionToken")) {
       if (((String)evt.getNewValue()).length() > 0) {
         addSessionToken((String)evt.getNewValue());
       }
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("listSymbols"))) {
       DefaultController.inform((String)evt.getNewValue());
     }
     else if ((evt.getPropertyName() != null) && (evt.getPropertyName().equals("showMenuView"))) {
       Logger.log("Main View pushing main view");
       this.controller.pushScreen(this);
     }
   }
 
   private void show() {
     this.controller.pushScreen(this);
   }
 
   private void hide() {
     this.controller.popScreen(this);
   }
 
   public boolean onClose()
   {
     this.controller.popScreen(this);
 
     return true;
   }
 
   protected void makeMenu(Menu menu, int instance) {
     menu.add(this.getAccountsMenuItem);
     menu.add(this.showListSymbolsScreenMenuItem);
     menu.add(this.listSymbolsMenuItem);
     menu.add(this.listSymbolsMultyMenuItem);
     menu.add(this.logoutMenuItem);
     menu.add(this.ratesMenuItem);
     menu.add(this.showRatesMenuItem);
     menu.add(this.optionsMenuItem);
     menu.add(this.helpMenuItem);
     menu.add(this.aboutMenuItem);
     menu.add(this.closeMenuItem);
   }
}

